package com.mico.example.springboot.config.domain;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

/**
 * Created by micocube
 * ProjectName: springboot-example
 * PackageName: com.mico.example.springboot.config.domain
 * User: micocube
 * Email: ldscube@gmail.com
 * CreateTime: 2019/2/25上午10:45
 * ModifyTime: 2019/2/25上午10:45
 * Version: 0.1
 * Description: 配置属性来自application.properties / application.yml
 * @Validated开启校验，javax.validation.constraints.*下的所有校验规则
 * 部分注解好像不生效？除了@NotEmpty，原因未知
 * 同理，使用控制器时，也可以用@Valid开启校验
 * public String addUser(@Valid Person user,BindingResult bindingResult){...}
 **/
@Data
@Component
@Validated
public class Person {
    @Value("${person.name}")
    @Size(min = 2, max = 20, message = "姓名长度必须大于 2 且小于 20 字")
    private String name;
    @Value("${person.age}")
    @NotEmpty(message = "年龄不允许为空")
    @Max(value = 150,message = "年龄最大不超过150")
    @Min(value = 11,message = "年龄需超过11")
    private Integer age;
    @Value("${person.desc}")
    private String desc;

}
